for(initialization;condition;evaluation) { statements ---------- ---------- } next statements
class demo { public static void main(String args[]) { for(int i=1;i<=10;i++) System.out.print(i+" "); } }
1 2 3 4 5 6 7 8 9 10
class demo { public static void main(String args[]) { for(int i=10;i>=1;i--) System.out.print(i+" "); } }
10 9 8 7 6 5 4 3 2 1
class demo { public static void main(String args[]) { for(int i=1;i<=100;i=i+2) System.out.print(i+" "); } }
class demo { public static void main(String args[]) { for(int i=1;i<=100;i++) if(i%2==0||i%3==0) System.out.print(i+" "); } }
class demo { public static void main(String args[]) { for(int i=1;i<=10;i++) System.out.print("Amravati"); } }
Amravati Amravati Amravati Amravati Amravati Amravati Amravati Amravati Amravati Amravati
import java.util.Scanner; class demo { public static void main(String args[]) { int s=0; Scanner scan=new Scanner(System.in); for(int i=1;i<=10;i++) { System.out.print("Enter a number"); int n=scan.nextInt(); s=s+n; } System.out.print("Sum is "+s); } }
Enter a number 5 Enter a number 6 Enter a number 2 Enter a number 9 Enter a number 3 Enter a number 4 Enter a number 1 Enter a number 7 Enter a number 8 Enter a number 3 Sum is 48
import java.util.Scanner; class demo { public static void main(String args[]) { int s1=0,s2=0; Scanner scan=new Scanner(System.in); for(int i=1;i<=10;i++) { System.out.print("Enter a number"); int n=scan.nextInt(); if(n%2==0) s1=s1+n; else s2=s2+n; } System.out.print("Sum of even is"+s1); System.out.print("Sum of odd is"+s2); } }
Enter a number 5 Enter a number 6 Enter a number 2 Enter a number 9 Enter a number 3 Enter a number 4 Enter a number 1 Enter a number 7 Enter a number 8 Enter a number 3 Sum of even nos is 20 Sum of odd nos is 28